package com.tianya.cas.config;

import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class CasConfig {
    @Value("${cas.server-url-prefix}")
    private String prefix;
    @Value("${cas.server-login-url}")
    private String login;
    @Value("${cas.client-host-url}")
    private String client;

    /**
     * description:授权过滤器
     */
    @Bean
    public FilterRegistrationBean filterAuthenticationRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new AuthenticationFilter());
        // 设定匹配的路径
        registration.addUrlPatterns("/*");
        Map<String,String> initParameters = new HashMap<>();
        initParameters.put("casServerLoginUrl", login);
        initParameters.put("serverName", client);
        //设置忽略  退出登录不用登录CAS
        initParameters.put("ignorePattern", "/system/*");
        registration.setInitParameters(initParameters);
        // 设定加载的顺序
        registration.setOrder(1);
        return registration;
    }
}
